package com.xteam.youin_supplier.adpter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.xteam.youin_supplier.R;
import com.xteam.youin_supplier.model.BaseInfo;


/**
 * Created by Curry on 2015-12-16.
 */
public class AddOilDetailListViewAdapter extends RecyclerView.Adapter<AddOilDetailListViewAdapter.ViewHolder> implements View.OnClickListener {

    private Context mContext;
    private BaseInfo baseInfo;
    public AddOilDetailListViewAdapter(Context context, BaseInfo baseInfo) {
        mContext = context;
        this.baseInfo = baseInfo;
    }
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    @Override
    public void onClick(View view) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            mOnItemClickListener.onItemClick(view,(int)view.getTag());
        }
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }


    //define interface
    public static interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, int position);
    }

    @Override
    public AddOilDetailListViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.add_oil_detail_list_title_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        //将创建的View注册点击事件
        view.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(AddOilDetailListViewAdapter.ViewHolder holder, int position) {
        holder.itemView.setTag(position);

        holder.getTime().setText(baseInfo.getData().getList().get(position).getTime());
        holder.getTeamName().setText(baseInfo.getData().getList().get(position).getName());
        holder.getNum().setText(baseInfo.getData().getList().get(position).getStr());
    }

    @Override
    public int getItemCount() {
        return baseInfo.getData().getList().size();
    }


    class ViewHolder extends RecyclerView.ViewHolder{
        private TextView time;
        private TextView teamName;
        private TextView num;
        public ViewHolder(View itemView) {
            super(itemView);
            time = (TextView) itemView.findViewById(R.id.time);
            teamName = (TextView) itemView.findViewById(R.id.team_name);
            num = (TextView) itemView.findViewById(R.id.num);
        }

        public TextView getTime() {
            return time;
        }

        public void setTime(TextView time) {
            this.time = time;
        }

        public TextView getTeamName() {
            return teamName;
        }

        public void setTeamName(TextView teamName) {
            this.teamName = teamName;
        }

        public TextView getNum() {
            return num;
        }

        public void setNum(TextView num) {
            this.num = num;
        }
    }
}
